import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const loginUser = JSON.parse(localStorage.getItem('user')) || {}

export default new Vuex.Store({
  state: {
    direction: 'forward',
    isLoading: false,
    // userHeader: loginUser.userHeader || require('../assets/third_guide_head.png'),
    userName: loginUser.userName || '游客',
    mHeader: {
      display: true,
      tab: []
    },
    headerHeight: 0,
    oHeader: {
      show: false,
      showAlertTab: false,
      num: 0,
      tab: [
        'download',
        'share',
        'delete',
        'rename',
        'more'
      ]
    }
  },
  mutations: {
    UPDATE_DIRECTION(state, param) {
      state.direction = param.direction
    },
    updateLoadingStatus(state, payload) {
      state.isLoading = payload.isLoading
    },
    updateUser(state, user) {
      // state.userHeader = loginUser.userHeader = user.userHeader
      state.userName = loginUser.userName = user.userName
      localStorage.setItem('user', JSON.stringify(loginUser))
    },
    updateMHeader(state, arg) {
      arg.kind.forEach((item, index) => {
        state.mHeader[item] = arg.param[index]
      })
    },
    showOHeader(state, arg) {
      if (arg) {
        state.oHeader.show = true
        document.getElementById('oheader').style.top = '0'
        document.getElementById('obottom').style.bottom = '0'
      } else {
        state.oHeader.show = false
        state.oHeader.showAlertTab = false
        document.getElementById('oheader').style.top = `-${state.headerHeight}px`
        document.getElementById('obottom').style.bottom = `-53px`
      }
    },
    updateHeaderHeight(state, arg) {
      state.headerHeight = arg
    },
    showBottomAlertTab(state, arg) {
      state.oHeader.showAlertTab = arg
    }
  },
  actions: {
    updateUser({ commit }, user) {
      commit('updateUser', user)
    },
    updateMHeader({ commit }, arg) {
      commit('updateMHeader', arg)
    },
    showOHeader({ commit }, arg) {
      commit('showOHeader', arg)
    },
    updateHeaderHeight({ commit }, arg) {
      commit('updateHeaderHeight', arg)
    },
    showBottomAlertTab({ commit }, arg) {
      commit('showBottomAlertTab', arg)
    }
  }
})
